<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增书籍信息')" />
    <th:block th:include="include :: summernote-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-bookinfo-add">
            
            <div class="form-group">
				<label class="col-sm-3 control-label">书籍分类：</label>
				<div class="col-sm-8">
					<select name="classId" class="form-control m-b" th:with="type=${@classification.getType('2')}">
	                    <option th:each="classification : ${type}" th:text="${classification.className}" th:value="${classification.id}"></option>
	                </select>
				</div>
			</div>
			<div class="form-group">    
                <label class="col-sm-3 control-label is-required">书籍名称：</label>
                <div class="col-sm-8">
                    <input name="bookName" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">作者：</label>
                <div class="col-sm-8">
                    <input name="author" class="form-control" type="text" required>
                </div>
            </div>          
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">书籍简介：</label>
                <div class="col-sm-8">
                    <input name="bookIntroduce" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">书籍价格：</label>
                <div class="col-sm-8">
                    <input name="price" class="form-control" type="number" required>
                </div>
            </div>
             <div class="form-group">    
                <label class="col-sm-3 control-label">是否付费：</label>
                <div class="col-sm-9">
				    <label class="radio-box">
				        <input type="radio" checked="true" value="0" id="optionsRadios1" name="isPay">免费
				    </label>
				    <label class="radio-box">
				        <input type="radio" value="1" id="optionsRadios2" name="isPay">付费
				    </label>
				</div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">书籍类型：</label>
                <div class="col-sm-9">
				    <label class="radio-box">
				        <input type="radio" checked="true" value="1" id="optionsRadios1" name="bookType">热门书籍
				    </label>
				    <label class="radio-box">
				        <input type="radio" value="2" id="optionsRadios2" name="bookType">其他书籍
				    </label>
				</div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">状态：</label>
                <div class="col-sm-9">
				    <label class="radio-box">
				        <input type="radio" checked="true" value="1" id="optionsRadios1" name="bookStatus">上架
				    </label>
				    <label class="radio-box">
				        <input type="radio" value="0" id="optionsRadios2" name="bookStatus">下架
				    </label>
				</div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">显示排序：</label>
                <div class="col-sm-8">
                    <input name="ord" class="form-control" type="number" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">书籍图片：</label>
                <div class="col-sm-8">
                    <input name="url" class="form-control" type="file"  onchange="uploadFile('url')">
                    <input id="imageUrl" name="imageUrl" class="form-control" type="hidden">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">书籍描述：</label>
                <div class="col-sm-8">
                    <input type="hidden" class="form-control" name="bookDescribe">
                    <div class="summernote" id="bookDescribe"></div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: summernote-js" />
    <script th:inline="javascript">
        var prefix = ctx + "system/bookinfo"
        $("#form-bookinfo-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-bookinfo-add').serialize());
            }
        }
        
        function uploadFile(filePath) {
            var formData = new FormData();
            if ($("input[name='" + filePath + "']")[0].files[0] == null) {
                $.modal.alertWarning("请先选择文件路径");
                return false;
            }
            formData.append('file', $("input[name='" + filePath + "']")[0].files[0]);
            $.ajax({
            	url: ctx + "common/upload",
                type: 'post',
                cache: false,
                data: formData,
                processData: false,
                contentType: false,
                dataType: "json",
                success: function (result) {
                	if (result.code == web_status.SUCCESS) {
                		$("#imageUrl").val(result.url);
					} else {
						$.modal.alertError(result.msg);
					}
                    
                }
            });
        }
        
        $(function() {
            $('.summernote').summernote({
            	placeholder: '请输入条文内容',
    			height : 200,
                lang: 'zh-CN',
                callbacks: {
                    onChange: function(contents, $edittable) {
                        $("input[name='" + this.id + "']").val(contents);
                    },
                    onImageUpload: function(files) {
                    	var obj = this;
                    	var data = new FormData();
                    	data.append("file", files[0]);
                    	$.ajax({
                    		type: "post",
                    		url: ctx + "common/upload",
                    		data: data,
                    		cache: false,
                    		contentType: false,
                    		processData: false,
                    		dataType: 'json',
                    		success: function(result) {
                    			if (result.code == web_status.SUCCESS) {
                    				$('#' + obj.id).summernote('insertImage', result.url);
                    			} else {
                    				$.modal.alertError(result.msg);
                    			}
                    		},
                    		error: function(error) {
                    			$.modal.alertWarning("图片上传失败。");
                    		}
                    	});
                    }
                }
            });
        });
    </script>
</body>
</html>